﻿//-----------------------------------------------------------------------
// <copyright file="GoatFieldFactory.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGen.Object
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Static factory which handles allocator a GoatField from a set of properties.
    /// </summary>
    public static class GoatFieldFactory
    {
        #region Fields - Private

        /// <summary>
        /// Allocator dictionary.
        /// </summary>
        private static IDictionary<string, Func<string, GoatFieldProperties, GoatField>> allocators =
            new Dictionary<string, Func<string, GoatFieldProperties, GoatField>>();

        #endregion

        #region Constructor - Static

        /// <summary>
        /// Static constructor which populates the field allocator dictionary.
        /// </summary>
        static GoatFieldFactory()
        {
            allocators.Add(GoatFieldConstants.StringFieldType, (name, properties) => new GoatFieldString(name, properties));
            allocators.Add(GoatFieldConstants.MatrixFieldType, (name, properties) => new GoatFieldMatrix2D(name, properties));
            allocators.Add(GoatFieldConstants.SizeFieldType, (name, properties) => new GoatFieldSize(name, properties));
            allocators.Add(GoatFieldConstants.ColorFieldType, (name, properties) => new GoatFieldColorRGBA(name, properties));
            allocators.Add(GoatFieldConstants.IntFieldType, (name, properties) => new GoatFieldInt(name, properties));
            allocators.Add(GoatFieldConstants.FloatFieldType, (name, properties) => new GoatFieldFloat(name, properties));
            allocators.Add(GoatFieldConstants.GUIDFieldType, (name, properties) => new GoatFieldGuid(name, properties));
            allocators.Add(GoatFieldConstants.ConfigFieldType, (name, properties) => new GoatFieldConfig(name, properties));
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Allocate a Goat field with the passed name using the provided properties.
        /// </summary>
        /// <param name="name">Name of the field.</param>
        /// <param name="properties">Properties to get data for the field from.</param>
        /// <returns>Returns the allocated Goat field.</returns>
        public static GoatField Allocate(string name, IDictionary<string, string> properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("values", "Values must be non-null.");
            }

            string type;
            if (!properties.TryGetValue(GoatFieldConstants.TypeKey, out type))
            {
                throw new GoatException("Missing type field.");
            }

            Func<string, GoatFieldProperties, GoatField> func;
            if (!allocators.TryGetValue(type, out func))
            {
                throw new GoatException("Failed to find field allocator for type: " + type);
            }

            GoatFieldProperties props = new GoatFieldProperties(properties);

            return func(name, props);
        }

        #endregion
    }
}
